//
//  HealthViewController.m
//  FoodList
//
//  Created by 王金晶 on 16/4/6.
//  Copyright © 2016年 王金晶. All rights reserved.
//

#import "HealthViewController.h"
#import "RequestManger.h"
#import "HealthModel.h"
#import <MJRefresh.h>
#import "HealthTableViewCell.h"
#import "HealthSecondViewController.h"
@interface HealthViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *modelArray;
@property(nonatomic,assign)NSInteger index;
@end

@implementation HealthViewController
- (void)getData:(NSString *)stringUrl
{   if(_modelArray == nil){
    _modelArray = [NSMutableArray array];
   }
    [RequestManger requestWithURL:stringUrl requestType:@"RequestGet"parDic:nil finish:^(NSData *data) {
        NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        [_modelArray removeAllObjects];
        for (NSDictionary *dic in array) {
            HealthModel *model = [[HealthModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            NSDictionary *logoDic = dic[@"logo"];
            [model setValuesForKeysWithDictionary:logoDic];
            if (![dic[@"view_count"] isKindOfClass:[NSNull class]]) {
                [model setValue:dic[@"view_count"] forKey:@"view_count1"];
            }
            [_modelArray addObject:model];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
        
    } error:nil];
  }
#pragma mark ---------下拉刷新 上拉加载-----------------
- (void)mjRefresh:(UITableView *)tableView
{
    tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _index = 1;
        [self getData:@"http://lohas.mamashai.com/api/articles?&count=20&login=false"];
        [tableView.mj_header endRefreshing];
    }];
    tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self getData:[NSString stringWithFormat:@"http://lohas.mamashai.com/api/articles?&page=%ld&count=20&login=false",_index + 1]];
        [tableView.mj_footer endRefreshing];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"养生";
    
    //首次加载数据
    [self getData:@"http://lohas.mamashai.com/api/articles?&count=20&login=false"];
    [self mjRefresh:_tableView];
    
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 1000;

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _modelArray.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HealthTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"health"];
    if (cell == nil) {
        cell = [[NSBundle mainBundle]loadNibNamed:@"HealthTableViewCell" owner:nil options:nil].firstObject;
        cell.icon.layer.cornerRadius = 10;
        cell.icon.layer.masksToBounds = YES;
    }
    cell.model = _modelArray[indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HealthSecondViewController  *healthSecondVc = [[HealthSecondViewController alloc]initWithNibName:@"HealthSecondViewController" bundle:nil];
    healthSecondVc.model = _modelArray[indexPath.row];
    [self.navigationController pushViewController:healthSecondVc animated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
